/**
 * Created by yongwang on 2015/7/21.
 */
//引用方式说明，id为input属性
// var test_list = [{name: "小张",id: "0"}, {name:"小苏",id:"1"}, {name:"小杨",id:"2"}, {name:"上海绿城",id:"3"}];
//new select(test_list,id);

(function(w){
    var select = function(data){
        this.currentObj = $(data.currentObj);
        this.init(data.data,data.class);
        this.evt(data.method);
        return this;
    }, c,inputClass = "",div = {},list;

    select.prototype = {
        init: function(list,clas){
            c = this;
            this.list = list;
            this.inputClass = clas;
            this.oldValue = $("."+clas).val();
            this.div = document.createElement("div");
            this.div.id = new Date().getTime();
            this.div.className = "cascading-div";
            c.currentObj.find("."+clas).after($(this.div));
            c.currentObj.find("."+clas).attr("style","padding: 8px;width: 150px;");
            $(this.div).attr("style","overflow-y: scroll;display: none;width: 150px;border: 1px #74c0f9 solid;background: #FFF;position: absolute;z-index: 10;color: #323232;max-height: 375px;");
            this.div.style.left = c.currentObj.find("."+clas)[0].offsetLeft + "px";
            this.div.style.top = c.currentObj.find("."+clas)[0].offsetTop + 37+ "px";
            $("body").css("transform","translate3d(0,0,0)");
            $("body").css("-webkit-transform","translate3d(0,0,0)");
            //-webkit-transform: translate3d(0,0,0);
        },
        AutoComplete: function(auto, search, mylist) {
            if (search.value.replace(/\s/g,"") != c.oldValue || c.oldValue == "") {
                var autoNode = $(auto); //弹出框
                var carlist = new Array(); //匹配的数组
                var n = 0;
                c.oldValue = search.value;
                //获取匹配数组
               /* mylist.forEach(function(item){
                    /!*  宝宝树66小区二次改造    mod   gct  20170802   start   *!/
                    var varName=item.name;
                    if(typeof(varName) == 'undefined')
                    {
                         varName=item.communityName;

                    }


                        if (varName.indexOf(c.oldValue) >= 0) {
                            carlist[n] = item;n++;
                        }
                    /!*  宝宝树66小区二次改造    mod   gct  20170802   end   *!/


                });*/
                /*  宝宝树66小区二次改造    mod   gct  20170915   start   */
                if(mylist != null) {
                    if(mylist.list != undefined) {
                        mylist = mylist.list;
                    }

                    mylist.forEach(function(item){
                        /*  宝宝树66小区二次改造    mod   gct  20170802   start   */
                        var varName=item.name;
                        if(typeof(varName) == 'undefined')
                        {
                            varName=item.communityName;

                        }


                        if (varName.indexOf(c.oldValue) >= 0) {
                            carlist[n] = item;n++;
                        }
                        /*  宝宝树66小区二次改造    mod   gct  20170802   end   */


                    });
                }  else {
                    return;
                }

            /*  宝宝树66小区二次改造    mod   gct  20170915   end   */


                if (carlist.length == 0) {//没有任何匹配
                    autoNode.hide();
                    return;
                }

                autoNode.empty();  //清空上次的记录
                carlist.forEach(function(item){
                    var wordNode = item.name;   //弹出框里的每一条内容
                    /*  宝宝树66小区二次改造    mod   gct  20170802   start   */
                    if(typeof(wordNode) == 'undefined')
                    {
                        wordNode=item.communityName;

                    }
                    /*  宝宝树66小区二次改造    mod   gct  20170802   end   */
                    var newDivNode = $("<span>").attr("data-id", item.objId);//设置每个节点的id值
                    newDivNode.attr("style", "display: block;font:14px/25px arial;height:25px;padding:0 8px;cursor: pointer;");
                    newDivNode.html(wordNode).appendTo(autoNode);  //追加到弹出框
                });
                autoNode.show();
            }
        },
        evt: function(method){
            //输入框获取焦点
            c.currentObj.find("."+c.inputClass).focus(function () {
                c.currentObj = $(this.parentNode);
                inputClass = this.className.split(" ")[1];
                div = this.nextSibling;
                if(inputClass.match("c-provinces")){
                    list = JSON.parse(sessionStorage.province);
                } else if(inputClass.match("c-citys")){
                    list = JSON.parse(sessionStorage.city);
                }else if(inputClass.match("c-cityArea")){
                    list = JSON.parse(sessionStorage.cityArea);
                }else if(inputClass.match("c-village")){
                    list = JSON.parse(sessionStorage.village);
                }
                if (this.value == "") {
                    c.AutoComplete(this.nextSibling, this, list);
                }
            });
            //键盘弹起
            c.currentObj.find("."+c.inputClass).keyup(function () {
                c.currentObj.find(".cascading-div").css("display","none");
                c.AutoComplete(this.nextSibling, this, list);
            });

            //点击页面隐藏自动补全提示框
            document.onclick = function (e) {
                //if($("#"+inputId).length > 0)div = $("#"+inputId)[0].nextSibling;
                var e = e ? e : window.event;
                var tar = e.srcElement || e.target;
                if (!tar.className.match(inputClass)) {
                    if ($(div).is(":visible")) {
                        $(div).css("display", "none");
                    }
                }
            };
            //鼠标点击获取文字
            $(document).on("click", "#"+c.div.id+" span", function (e) {
                e.preventDefault();
                c.currentObj.find("."+inputClass).val(this.innerText);
                c.currentObj.find("."+inputClass)[0].dataset.cId = this.dataset.id;
                if(method)method(this.dataset.id);
                if(inputClass.match("c-provinces")){//小区级联专用，可封装移除
                    c.currentObj.find(".c-citys").val("");
                    c.currentObj.find(".c-citys")[0].dataset.cId = "";
                    c.currentObj.find(".c-cityArea").val("");
                    c.currentObj.find(".c-cityArea")[0].dataset.cId = "";
                    if(c.currentObj.find(".c-village").length > 0){
                        c.currentObj.find(".c-village").val("");
                        c.currentObj.find(".c-village")[0].dataset.cId = "";
                    }
                }else if(inputClass.match("c-citys")){
                    c.currentObj.find(".c-cityArea").val("");
                    c.currentObj.find(".c-cityArea")[0].dataset.cId = "";
                    if(c.currentObj.find(".c-village").length > 0){
                        c.currentObj.find(".c-village").val("");
                        c.currentObj.find(".c-village")[0].dataset.cId = "";
                    }
                }else if(inputClass.match("c-cityArea")){
                    if(c.currentObj.find(".c-village").length > 0){
                        c.currentObj.find(".c-village").val("");
                        c.currentObj.find(".c-village")[0].dataset.cId = "";
                    }
                }
            });

            c.div.addEventListener("mouseover",function(e){
                if(e.target.tagName == "SPAN"){
                    e.target.style.background = "#ebebeb";
                }
            },false);
            c.div.addEventListener("mouseout",function(e){
                if(e.target.tagName == "SPAN"){
                    e.target.style.background = "#fff";
                }
            },false);
            $(document).on("input",".city-cascading input",function(){
                if(this.value == ""){
                    this.dataset.cId = "";
                }
            })
        }
    };
    w.select = select;
}(window));